package com.ang.util;

import com.spire.doc.Document;
import com.spire.doc.PrivateFontPath;
import com.spire.doc.ToPdfParameterList;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ssang
 * @date 2023-04-21 11:15
 * dependency:  poi-tl;spire.doc.free;
 */
public class PdfUtils {

    public static boolean wordToPdf(String wordFilePath, String pdfFilePath){
        Document document = new Document();
        document.loadFromFile(wordFilePath);

        //字体所在目录
        String path = "";
        //判断启动应用程序的操作系统
        if (System.getProperty("os.name").toLowerCase().indexOf("linux") >= 0) {
            path = File.separator + "usr" + File.separator + "share" + File.separator + "fonts" + File.separator;
        } else if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
            path = "C:" + File.separator + "Windows" + File.separator + "Fonts" + File.separator;
        }

        PrivateFontPath privateFontPath= new PrivateFontPath();
        privateFontPath.setFontPath(path);
        List<PrivateFontPath> privateFontPathList = new ArrayList<>();
        privateFontPathList.add(privateFontPath);
        ToPdfParameterList toPdfParameterList = new ToPdfParameterList();
        toPdfParameterList.setPrivateFontPaths(privateFontPathList);

        document.saveToFile(pdfFilePath, toPdfParameterList);
        return false;
    }

}
